package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.mvvm.model.AddPatrolModel;
import com.bwie.parkdemo.mvvm.model.entity.GroupEntity;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.entity.PointEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 添加巡更路线ViewModel
 * */
public class AddPatrolViewModel extends BaseOBViewModel<AddPatrolModel>{

    private List<Object>groupDatas;//组的集合
    public ObservableField<List<Object>>pointDatas;//点的单项数据绑定集合
    public AddPatrolClickListener clickListener;//分组按钮监听器
    public AddPatrolItemClickListener itemClickListener;//点位item监听
    public ObservableField<String>groupName;
    private LoginEntity.Values userEntity;
    public int headlayout = R.layout.layout_patrol_head;
    public int footlayout = R.layout.layout_patrol_foot;

    public AddPatrolViewModel(@NonNull Application application) {
        super(application);
        pointDatas = new ObservableField<>();
        groupName = new ObservableField<>("第一小队");
        groupDatas = new ArrayList<>();
        clickListener = new AddPatrolClickListener();
        itemClickListener = new AddPatrolItemClickListener();
    }

    @Override
    protected AddPatrolModel createModel() {
        return new AddPatrolModel();
    }

    @Override
    protected void initData() {
        userEntity = m.requestUser(getApplication());
        //获取全部巡逻小组
        m.request(null).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
        //获取全部巡逻点
        m.requestPointes().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void addPoint(String pointName){
        Log.e("ZXY","addPoint");
        if (TextUtils.isEmpty(pointName)) {
            showMsg("请输入巡逻点");
            return;
        }
        Map<String,String>map = new HashMap<>();
        map.put("pointName",pointName);
        String jsonStr = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        m.requestAddPoint(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        if (baseEntity instanceof PointEntity){
            PointEntity entity = (PointEntity) baseEntity;
            pointDatas.set(entity.getDatas());
        }else if (baseEntity instanceof GroupEntity){
            GroupEntity entity = (GroupEntity) baseEntity;
            groupDatas.addAll(entity.getDatas());
        }else {
            showMsg(baseEntity.msg);
            if (baseEntity.statuesCode.equals("200")){
                //刷新巡更点列表
                m.requestPointes().subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this);
            }
        }
    }

    //当前界面点击按钮监听器
    public class AddPatrolClickListener{
        //分组点击
        public void groupClick(){
            Log.e("ZXY","AddPatrolClickListener:groupClick()");
        }
        //清空点击事件
        public void clearClick(){
            Log.e("ZXY","AddPatrolClickListener:clearClick()");
        }
        //确定提交点击事件
        public void subClick(){
            /**
             * {
             "pend": "string",
             "pgroupId": 0,
             "pid": 0,
             "ppoint": "string",
             "pstart": "string",
             "ptime": "string",
             "ptype": 0,
             "puserId": 0
             }
             * */
            //ppoint->拼接为一个JSONArray[{"pointId":"pointName","":""},{}]
        }
    }

    //当前界面recyclerview点击事件
    public class AddPatrolItemClickListener implements BaseQuickAdapter.OnItemClickListener{

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            //判断当前点击的条目是否为+->为+时弹出添加巡更地点的dialog并退出当前方法
            if (position == (pointDatas.get().size()-1)){
                Log.e("ZXY","AddPatrolItemClickListener");
                EventBus.getDefault().post(""+System.currentTimeMillis());
                return;
            }
            //切换点选择状态
            List<Object>list = new ArrayList<>();
            list.addAll(pointDatas.get());
            PointEntity.Values values = (PointEntity.Values) list.get(position);
            values.isSelect = !values.isSelect;
            list.set(position,values);
            pointDatas.set(list);
        }
    }

}
